﻿#include "experimentinfowindow.h"
#include "ui_experimentinfowindow.h"
extern intListener doorStatus;
experimentInfoWindow::experimentInfoWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::experimentInfoWindow)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/pic/picture/kafavicon.ico"));

    QRegExp regExp("^[\u4E00-\u9FA5a-zA-Z0-9_\\+\\-\\\\ \\/ \\( \\) \\.\\[ \\] \\<\\>]+$");

    QRegExpValidator *validator = new QRegExpValidator(regExp, this);
    ui->ExperimentName->setValidator(validator);
    ui->ExperimentName->setMaxLength(30);

    QRegExp regExp1("^[\u4E00-\u9FA5a-zA-Z0-9_]+$");
    QRegExpValidator *validator1 = new QRegExpValidator(regExp1, this);
    ui->Code->setValidator(validator1);
    ui->Code->setMaxLength(30);
    ui->pushButton_3->setStyleSheet("background-color: rgba(0, 0, 0, 0.3);");
    ui->pushButton_3->setEnabled(false);
}


experimentInfoWindow::~experimentInfoWindow()
{
    delete ui;
}

void experimentInfoWindow::showEvent(QShowEvent *event)
{
    tcpClient=TcpClient::getInstance();

    ui->label->setText("<font color=red>*</font><font>实验名称</font>");
    ui->label_2->setText("<font color=red>*</font><font>扩增程序</font>");

    ui->label_8->hide();

    ui->System->blockSignals(true);
    ui->System->clear();
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QString sql="Select Name from ItemCode Where DicCode='4'";
    query.exec(sql);
    while (query.next()) {
        ui->System->addItem(query.value("Name").toString());
    }

    ui->System->blockSignals(false);

    ui->System_2->blockSignals(true);
    ui->System_2->clear();

    sql="Select Name from ItemCode Where DicCode='12' order by IsDefault desc";
    query.exec(sql);
    while (query.next()) {
        ui->System_2->addItem(query.value("Name").toString());
    }

    ui->System_2->blockSignals(false);

    ui->AmplProgram->blockSignals(true);
    ui->AmplProgram->clear();
    if(MainWindow::experimentStatus.get()==4){
        sql="Select programName from AmplProgramTemp order by isDefault desc";
    }else{
        sql="Select programName from AmplProgram order by isDefault desc";
    }

    query.exec(sql);
    while (query.next()) {
        ui->AmplProgram->addItem(query.value("ProgramName").toString());
    }

    ui->AmplProgram->blockSignals(false);

    if(MainWindow::experimentStatus.get()==4){
        ui->pushButton_9->show();
        ui->pushButton_10->show();

        ui->pushButton->setEnabled(false);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_4->setEnabled(false);

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType From ExperimentInfo e "
            "Join AmplProgramTemp a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
        }
    }
    else if(MainWindow::experimentStatus.get()==1){
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType From ExperimentInfo e "
            "Join AmplProgram a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
        }
    }
    else if(MainWindow::experimentStatus.get()==5){
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        sql="Select e.Id,ExperimentName,System,Code,ProgramName,ConsumableType From ExperimentInfo e "
            "Join AmplProgram a on a.id=e.AmplGuid";
        query.exec(sql);
        while (query.next()) {
            MainWindow::experimentGuid=query.value("ID").toString();
            ui->ExperimentName->setText(query.value("ExperimentName").toString());
            ui->AmplProgram->setCurrentText(query.value("ProgramName").toString());
            ui->Code->setText(query.value("Code").toString());
            ui->System->setCurrentText(query.value("System").toString());
        }
    }
    else{
        ui->pushButton_9->hide();
        ui->pushButton_10->hide();

        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(true);
        ui->pushButton_4->setEnabled(true);

        sql="Select Name from ItemCode where DicCode='4' and IsDefault='1'";
        query.exec(sql);
        while (query.next()) {
            ui->System->setCurrentText(query.value("Name").toString());
        }

        MainWindow::experimentGuid = common->GetGuid();
        sql=QString("Delete from ExperimentInfo");
        query.exec(sql);
        query.clear();

        QDateTime dateTime = QDateTime::currentDateTime();
        QString experimentName=QString::number(dateTime.date().year())+GetDateString(dateTime.date().month())+GetDateString(dateTime.date().day())+GetDateString(dateTime.time().hour())+GetDateString(dateTime.time().minute())+GetDateString(dateTime.time().second());
        sql=QString("Insert into ExperimentInfo values ("
                    "'%1','%2',(select id from AmplProgram where IsDefault='True'),'','','','未启动','','')").arg(MainWindow::experimentGuid,experimentName);
        query.exec(sql);
        query.clear();

        ui->ExperimentName->setText(experimentName);
        ui->Code->setText("");
    }
}

QString experimentInfoWindow::GetDateString(int val){
    return QString::number(val).length()==2?QString::number(val): "0"+QString::number(val);
}

void experimentInfoWindow::on_pushButton_clicked()
{
    if(ui->AmplProgram->currentText().isNull() ||ui->AmplProgram->currentText().isEmpty()){
        tcts *tctsWindow = new tcts();
        tctsWindow->infochange("提示","info","请先创建扩增程序",false,true);
        tctsWindow->show();
        return;
    }
    if(!ui->ExperimentName->text().isEmpty()){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);

        QString experimentName=ui->ExperimentName->text();
        QString code=ui->Code->text();
        QString programName=ui->AmplProgram->currentText();
        QString system=ui->System->currentText();
        QString sql=QString("Update ExperimentInfo set ExperimentName='%1', "
                            "code='%2',"
                            "AmplGuid=(select id from AmplProgram where programName='%3'),"
                            "System='%4',"
                            "ConsumableType='' "
                            "where id='%5'").arg(experimentName,code,programName,system,MainWindow::experimentGuid);

        query.exec(sql);
        query.clear();

        if(MainWindow::experimentStatus.get()==0){

            sql=QString("Delete from SampleInfo");
            query.exec(sql);

            sql=QString("Delete from SampleAccessInfo");
            query.exec(sql);

            for(int i=0;i<4;i++){
                for(int j=0;j<6;j++){
                    QString sampleId=common->GetGuid();
                    sql=QString("Insert into SampleInfo values("
                                "'%1',"
                                "'%2',"
                                "'%3',"
                                "'%4',"
                                "'',"
                                "'',"
                                "'','%5')").arg(sampleId,MainWindow::experimentGuid,QString::number(i),QString::number(j),QString::number(j*4+i+1));
                    query.exec(sql);
                }
            }
        }
        this->close();
        emit changeSampleWindow();
    }
}

void experimentInfoWindow::on_pushButton_2_clicked()
{
    this->close();
    if(MainWindow::experimentStatus.get() == 5){
        MainWindow::experimentStatus.set(0);
    }
    emit changeMainWindow();
}

void experimentInfoWindow::on_pushButton_4_clicked()
{
    this->close();
    emit changeMainWindow();
}

void experimentInfoWindow::on_ExperimentName_textChanged(const QString &arg1)
{
    if(arg1.isEmpty()){
        ui->label_8->show();
    }else {
        ui->label_8->hide();
    }
}

void experimentInfoWindow::on_pushButton_9_clicked()
{
    this->close();
    emit changeDataAnalysisWindow();
}

void experimentInfoWindow::on_pushButton_10_clicked()
{
    this->close();
    emit changeSampleWindow();
}

void experimentInfoWindow::on_pushButton_5_clicked()
{
    if(ui->AmplProgram->currentText().isNull() ||ui->AmplProgram->currentText().isEmpty()){
        tcts *tctsWindow = new tcts();
        tctsWindow->infochange("提示","info","请先创建扩增程序",false,true);
        tctsWindow->show();
        return;
    }
    QString id;
    QString sql;
    if(MainWindow::experimentStatus.get()==4){
        sql=QString("Select ID From AmplProgramTemp where ProgramName='%1'").arg(ui->AmplProgram->currentText());
    }
    else
    {
        sql=QString("Select ID From AmplProgram where ProgramName='%1'").arg(ui->AmplProgram->currentText());
    }

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    query.exec(sql);
    while(query.next()){
        id=query.value("ID").toString();
    }
    this->close();
    emit changeAmplProgramStepWindow(id);
}

